#!/usr/bin/env python
# -*- coding:UTF-8 -*-
"""
singleton.py
"""

import threading  # 导入线程模块

class Singleton(object):  # 饿汉式单例

    _instance = None  # 判断对象是否实例化过
    _instance_lock = threading.Lock()  # 用于线程锁定

    def __new__(cls):  # 创建对象时
        cls._instance_lock.acquire()  # 锁定线程，确保每次只被一个线程访问下面的代码
        try:
            if cls._instance is None:  # 如果对象从未实例化过
                cls._instance = super().__new__(cls)  # 实例化该对象
            return cls._instance  # 返回对象实例
        finally:
            cls._instance_lock.release()  # 解锁线程
